using DA.AnimGraph;
using UnityEngine;
using UnityEngine.UIElements;

namespace DA.AnimGraph.Editor {
    public class PreviewAGTree : VisualElement {
        private readonly PreviewEnv previewEnv;

        private ListView listView;

        public PreviewAGTree(PreviewEnv previewEnv) {
            this.previewEnv = previewEnv;
            listView = new ListView() {
                reorderable = false,
                selectionType = SelectionType.Single,
                showAlternatingRowBackgrounds = AlternatingRowBackground.All,
                fixedItemHeight = 60,
            };
            listView.makeItem = () => {
                var field = new PreviewAGTreeEntry(previewEnv);
                return field;
            };
            listView.bindItem += (element, index) => {
                var field = element as PreviewAGTreeEntry;
                field.SetData(previewEnv.agTree[index]);
            };

            previewEnv.OnConnect += OnConnect;
            previewEnv.ConnectUpdate += OnConnect;
            previewEnv.OnDisconnect += OnDisconnect;
        }
        private void OnConnect(AGEPreviewConnection connection) {
            previewEnv.agTree = connection.GetAGTree();
            DrawAgTree();
        }
        void OnDisconnect() {
            Clear();
        }

        private void DrawAgTree() {
            Clear();
            listView.itemsSource = previewEnv.agTree.data;
            Add(listView);
        }

    }

    public class PreviewAGTreeEntry : VisualElement {
        private readonly PreviewEnv previewEnv;
        private VisualElement c0, c1;
        private Label label0, label1;
        private GenericMenuButton addGraphBtn;
        public PreviewAGTreeEntry(PreviewEnv previewEnv) {
            this.previewEnv = previewEnv;
            style.flexDirection = FlexDirection.Column;
            c0 = new VisualElement();
            c0.style.flexDirection = FlexDirection.Row;
            c1 = new VisualElement();
            c1.style.flexDirection = FlexDirection.Row;

            label0 = new Label();
            label1 = new Label();
            addGraphBtn = new GenericMenuButton($"▼");
        }
        public void SetData(AGTreeEntry agTreeEntry) {
            c0.style.marginLeft = 20 * agTreeEntry.level;
            c1.style.marginLeft = 20 * agTreeEntry.level;
            bool isRoot = agTreeEntry.slotName == "root";

            // if (!agTreeEntry.isFill) {
            //     label0.text = $"({agTreeEntry.slotName})";
            // } else {
            //     label0.text = $"({agTreeEntry.slotName}) {agTreeEntry.name0}";
            //     label1.text = $"({agTreeEntry.slotName}) {agTreeEntry.name1}";
            // }
            label0.text = $"({agTreeEntry.slotName}) {agTreeEntry.name0}";
            label1.text = $"({agTreeEntry.slotName}) {agTreeEntry.name1}";

            if (agTreeEntry.curPort == 0) {
                label0.style.unityFontStyleAndWeight = FontStyle.BoldAndItalic;
                label1.style.unityFontStyleAndWeight = FontStyle.Normal;
            } else if (agTreeEntry.curPort == 1) {
                label0.style.unityFontStyleAndWeight = FontStyle.Normal;
                label1.style.unityFontStyleAndWeight = FontStyle.BoldAndItalic;
            } else {
                label0.style.unityFontStyleAndWeight = FontStyle.Normal;
                label1.style.unityFontStyleAndWeight = FontStyle.Normal;
            }

            var ageWindows = AGEGlobal.GetInstance().GetEnableWindows();
            foreach (var window in ageWindows) {
                addGraphBtn.AddMenuItem(window.titleText, () => SelectGraph(agTreeEntry.slotName, window.GetContext()));
            }

            Clear();
            Add(addGraphBtn);
            Add(c0);
            c0.Add(label0);
            if (!isRoot) {
                Add(c1);
                c1.Add(label1);
            }
        }

        void SelectGraph(string slotName, AGEContext context) {
            previewEnv.AddGraphToConnectionByContext(context, slotName);
        }
    }
}
